import type { Directive } from 'vue'
import { usePermissionStore } from '@/stores/permission'

export const permission: Directive = {
  mounted: (el, binding) => {
    const permissionStore = usePermissionStore()
    const permission = permissionStore.permission
    const { value } = binding
    const buttonPermissions = permissionStore.buttonPermissions
    if (!value || !buttonPermissions.length) return
    if (buttonPermissions.includes(value)) {
      // 有权限
      return true
    } else {
      // 没有权限
      ;(el as HTMLDivElement).parentElement?.removeChild(el)
    }
  }
}
